package wwk.com.android_meilishuo.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.wwk.core.decoration.SpacesItemDecoration;
import com.wwk.core.pullrefresh.Container_Adapter;
import com.wwk.core.pullrefresh.ListView;
import com.wwk.core.pullrefresh.core.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import wwk.com.android_meilishuo.R;
import wwk.com.android_meilishuo.mvp.presenter.HomePresenter;
import wwk.com.android_meilishuo.mvp.view.MLSView;
import wwk.com.android_meilishuo.ui.home.adapter.ChannelAdapter;
import wwk.com.android_meilishuo.ui.home.adapter.TrendAdapter;
import wwk.com.android_meilishuo.ui.secondfloor.EmptyLayout;
import wwk.com.android_meilishuo.ui.secondfloor.SecondFloorActivity;

/**
 * Created by Administrator on 2016/12/26.
 */

public class FragmentHome extends Fragment implements MLSView.IHomeChannelView {
    private Context mContext = null;

    private ListView list_Container;
    private ScrollView scrollView_home;
    private RelativeLayout relativeLayout_home;
    private LinearLayout linearLayout_home;
    //无网络加载空白布局
    private EmptyLayout emptyLayout_home;
    private RecyclerView recycler_home_channel, recyclerView_home_trend;
    private HomePresenter homePresenter;
    private ImageView imageView_beauty_banner, imageView_beauty_list1, imageView_beauty_list2, imageView_beauty_list3;
    private ImageView imageView_home_trend_title;
    private TextView textView_tomorrow,textView_trend_time,textView_trend_notice;
    //----------------------------------------------------
    //这部分没有实际显示数据的作用
    public ArrayList<Integer> listData = new ArrayList<>();
    int count = 10;
    Container_Adapter mAdapter;

    //----------------------------------------------------
    public static FragmentHome newInstance() {
        Bundle args = new Bundle();
        FragmentHome fragment = new FragmentHome();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        homePresenter = new HomePresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        loadDate(true);
        setListener();
        scrollView_home = new ScrollView(mContext);
        relativeLayout_home = new RelativeLayout(mContext);
        linearLayout_home = new LinearLayout(mContext);
        emptyLayout_home = new EmptyLayout(mContext);
        linearLayout_home.setOrientation(LinearLayout.VERTICAL);
        //list_Container.getListView().addHeaderView(scrollView_home);
        list_Container.getListView().addHeaderView(relativeLayout_home);
        //scrollView_home.addView(relativeLayout_home);
        if (EmptyLayout.isConnectivity(mContext)) {
            addLEView();
        } else {
            relativeLayout_home.addView(emptyLayout_home);
            emptyLayout_home.setOnLayoutClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (EmptyLayout.isConnectivity(mContext)) {
                        relativeLayout_home.removeView(emptyLayout_home);
                        addLEView();
                    }
                }
            });
        }
    }

    private void addLEView() {
        relativeLayout_home.addView(linearLayout_home);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //每日上新
        View view_channel = inflater.inflate(R.layout.home_channel_recyclerview, linearLayout_home, false);
        //美丽优选
        View view_beauty = inflater.inflate(R.layout.home_beauty_view, linearLayout_home, false);
        //潮流解析标题
        View view_trend_title = inflater.inflate(R.layout.home_trend_view_title, linearLayout_home, false);
        //潮流解析
        View view_trend = inflater.inflate(R.layout.home_trend_view, linearLayout_home, false);
        addChannelView(view_channel);
        List<ImageView> list_beauty = new ArrayList<>();
        List<TextView> list_Trend=new ArrayList<>();
        addBeautyView(view_beauty, list_beauty);
        imageView_home_trend_title = (ImageView) view_trend_title.findViewById(R.id.imageView_home_trend_title);
        addTrendView(view_trend,list_Trend);
        homePresenter.loadHomeData(mContext, list_beauty, imageView_home_trend_title,list_Trend);
        linearLayout_home.addView(view_channel, 0);
        linearLayout_home.addView(view_beauty, 1);
        linearLayout_home.addView(view_trend_title, 2);
        linearLayout_home.addView(view_trend, 3);
    }

    /**
     * 潮流解析布局
     *
     * @param view_trend
     */

    private void addTrendView(View view_trend,List<TextView> list_Trend) {
        recyclerView_home_trend = (RecyclerView) view_trend.findViewById(R.id.recyclerView_home_trend);
        textView_tomorrow= (TextView) view_trend.findViewById(R.id.textView_tomorrow);
        textView_trend_time= (TextView) view_trend.findViewById(R.id.textView_trend_time);
        textView_trend_notice= (TextView) view_trend.findViewById(R.id.textView_trend_notice);
        list_Trend.add(textView_tomorrow);
        list_Trend.add(textView_trend_time);
        list_Trend.add(textView_trend_notice);
        recyclerView_home_trend.setHasFixedSize(true);
        recyclerView_home_trend.setNestedScrollingEnabled(false);
        recyclerView_home_trend.addItemDecoration(new SpacesItemDecoration(15));
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerView_home_trend.setLayoutManager(layoutManager);
    }


    /**
     * 添加channel布局
     *
     * @param
     */
    private void addChannelView(View view_channel) {
        recycler_home_channel = (RecyclerView) view_channel.findViewById(R.id.recycler_home_channel);
        recycler_home_channel.setHasFixedSize(true);
        recycler_home_channel.setNestedScrollingEnabled(false);
        recycler_home_channel.addItemDecoration(new SpacesItemDecoration(15));
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recycler_home_channel.setLayoutManager(layoutManager);
    }

    /**
     * 美丽优选
     *
     * @param view_beauty
     * @param list
     */

    private void addBeautyView(View view_beauty, List<ImageView> list) {
        imageView_beauty_banner = (ImageView) view_beauty.findViewById(R.id.imageView_beauty_banner);
        imageView_beauty_list1 = (ImageView) view_beauty.findViewById(R.id.imageView_beauty_list1);
        imageView_beauty_list2 = (ImageView) view_beauty.findViewById(R.id.imageView_beauty_list2);
        imageView_beauty_list3 = (ImageView) view_beauty.findViewById(R.id.imageView_beauty_list3);
        list.add(imageView_beauty_banner);
        list.add(imageView_beauty_list1);
        list.add(imageView_beauty_list2);
        list.add(imageView_beauty_list3);
    }

    private void initView(View view) {
        for (int i = 0; i < count; i++) {
            listData.add(i);
        }
        list_Container = (ListView) view.findViewById(R.id.home_listView);
        mAdapter = new Container_Adapter(mContext, listData, count);
        list_Container.getListView().setAdapter(mAdapter);
    }

    private void setListener() {
        list_Container.setLoadDataListener(new PullToRefreshLayout.LoadDataListener() {
            @Override
            public void onRefresh() {
                //模拟1秒的时间加载数据
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadDate(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {
                //模拟1秒的时间加载数据
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadDate(false);
                    }
                }, 1000);
            }

            @Override
            public void onGoToSecond() {
                startActivity(new Intent(mContext, SecondFloorActivity.class));
            }
        });
    }


    /**
     * 模拟加载数据
     *
     * @param needFresh 是否需要刷新清空数据还是继续增加数据
     */
    public void loadDate(final boolean needFresh) {

        //刷新，则清空数据
        if (needFresh) {
            listData.clear();
        }
        //将新数据全部加入
        for (int i = 0; i < 0; i++) {
            listData.add(i);
        }
        mAdapter.notifyDataSetChanged();
        //是否需要更新和是否还有更多内容
        list_Container.onLoadComplete(needFresh, true);

    }


    @Override
    public void toSetAdapter(ChannelAdapter adapter) {
        recycler_home_channel.setAdapter(adapter);
    }

    @Override
    public void toSetTrendAdapter(TrendAdapter adapter) {
        recyclerView_home_trend.setAdapter(adapter);
    }

    @Override
    public void showError(Exception e) {

    }
}
